from presenter import basepresenter
from config import timezone

class TopicPresenter(basepresenter.BasePresenter):

    def respondToGet(self):
        #get the comments for the topic
        if (self.request.get('topic')):
            thetopic = self.discussiontask.getTopicByKey(self.request.get('topic'))
            thetopic.createdate = thetopic.createdate.replace(tzinfo=timezone.utc_tzinfo()).astimezone(timezone.site_tzinfo())
            self.template_values['topictitle'] = thetopic.title
            self.template_values['topicmessage'] = thetopic.message
            heading = thetopic.title
            self.template_values['heading'] = heading
            self.template_values['topickey'] = self.request.get('topic')
            self.template_values['topic'] = thetopic
            if (thetopic.type == 'Discussion'):
                self.template_values['discussion'] = thetopic.discussion
            if (thetopic.type == 'Article'):
                self.template_values['article'] = thetopic.article
            if (thetopic.type == 'Document'):
                self.template_values['document'] = thetopic.document
            comments = self.discussiontask.getCommentsByTopic(thetopic)
            for comment in comments:
                comment.date = comment.date.replace(tzinfo=timezone.utc_tzinfo()).astimezone(timezone.site_tzinfo())

        self.template_values['comments'] = comments

        # set up the page title
        page_title = self.sitetitle + ' - Discussion Post - ' + heading
        self.template_values['page_title'] = page_title

        #get the view to draw the page
        self.view.showGetResponse(self.template_values)